package br.com.gestacard.sync.persistencia.hibernate;

import org.hibernate.Query;
import org.hibernate.Session;

import br.com.gestacard.sync.entidade.AppBaseEntity;

import com.powerlogic.jcompany.comuns.PlcException;
import com.powerlogic.jcompany.persistencia.hibernate.PlcBaseHibernateDAO;

public class AppBaseDAO extends PlcBaseHibernateDAO {

	public boolean validaCodigoCadastrado(String entidade, Long id)
			throws PlcException {
		Session session = getSession();

		try {
			StringBuffer sbData = new StringBuffer(" from " + entidade
					+ " obj where obj.id = :id ");
			Query qry = session.createQuery(sbData.toString());
			qry.setParameter("id", id);

			return !qry.list().isEmpty();

		} catch (Exception e) {
			throw new PlcException("jcompany.erro.generico", new Object[] {
					"validaCodigoCadastrado", e }, e, log);
		}
	}

	public AppBaseEntity buscaPorId(String entidade, Long id) throws PlcException {
		Session session = getSession();

		try {
			StringBuffer sbData = new StringBuffer(" from " + entidade
					+ " obj where obj.id = :id ");
			Query qry = session.createQuery(sbData.toString());
			qry.setParameter("id", id);

			return (AppBaseEntity) qry.uniqueResult();

		} catch (Exception e) {
			throw new PlcException("jcompany.erro.generico", new Object[] {
					"validaCodigoCadastrado", e }, e, log);
		}
	}
}
